package com.ibsol.control
{
	import com.ibsol.service.SQLExecuteService;
	
	import flash.errors.SQLError;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	public class SQLUtil
	{
		public function SQLUtil()
		{
		}
		
		public function exeSQLFile(path:String):void
		{
			var XMLReq:URLRequest = new URLRequest(path);
			var XMLLoader:URLLoader = new URLLoader(XMLReq);
			XMLLoader.addEventListener(Event.COMPLETE, function(e:Event):void
			{
				var inputXML:XML = new XML();
				var xmlStr:String = XMLLoader.data.toString();
				//Deal with massy code
				var re:RegExp = new RegExp("<\/sql>", "i");
				inputXML = XML(xmlStr.split(re)[0] + "<\/sql>");
				var total:uint = inputXML.children().length();
				var sqlService:SQLExecuteService = new SQLExecuteService();
				for(var i:uint = 0; i < total; i++)
				{
					try{
						sqlService.executeSQL(inputXML.statement[i].toString());
					}catch(e:SQLError){continue;}
				}
			});
		}
		
		public function executeXML(inputXML:XML):void
		{
			var total:uint = inputXML.children().length();
			var sqlService:SQLExecuteService = new SQLExecuteService();
			var info:String;
			for(var i:uint = 0; i < total; i++)
			{
				try{
					sqlService.executeSQL(inputXML.statement[i].toString());
				}catch(e:SQLError){
					continue;
				}
			}
		}

	}
}